/**   
 *    
 * 项目名称：SmartKungFuService   
 * 类名称：OrderDeleteServlet   
 * 类描述：   订单删除
 * 创建人：zhanghl   
 * 创建时间：2016年11月28日 下午4:24:22   
 * 修改人：zhanghl   
 * 修改时间：2016年11月28日 下午4:24:22   
 * 修改备注：   
 * @version    
 *    
 */
package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.OrderReqPara;
import com.lst.entity.response.OrderResPara;
import com.lst.model.ClientGrowthUsed;
import com.lst.model.ClientRoll;
import com.lst.model.MstOrder;
import com.lst.model.OrderDetail;
import com.lst.service.ClientGrowthUsedService;
import com.lst.service.ClientRollService;
import com.lst.service.MstOrderService;
import com.lst.service.OrderDetailService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: OrderDeleteServlet
 * @Description: 订单删除
 * @author zhanghl
 * @date 2016年11月28日 下午4:24:22
 *
 */
public class OrderDeleteServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	private OrderDetailService orderDetailService = ctx.getBean(OrderDetailService.class);

	private MstOrderService mstOrderService = ctx.getBean(MstOrderService.class);

	private ClientRollService clientRollService = ctx.getBean(ClientRollService.class);

	private ClientGrowthUsedService clientGrowthService = ctx.getBean(ClientGrowthUsedService.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		logger.info("doPost begin: " + startDate);

		OrderResPara resPara = new OrderResPara();

		try {
			OrderReqPara reqPara = getReqPara(request);

			if(isCheckField(reqPara,resPara)){
				Integer odid = Integer.valueOf(reqPara.getOdid());
				Integer clientid = Integer.valueOf(reqPara.getUserid());

				OrderDetail od = orderDetailService.selectByPrimaryKey(odid);

				MstOrder mo = mstOrderService.selectByPrimaryKey(od.getOrderid());

				if(CommCode.SYSTEM_ORDER_STATUS_PAY.equals(od.getOrderstatus())){//待付款

//					if(!StringUtils.isEmpty(od.getReserved1())){//用户等级优惠使用ID
//
//						ClientGrowthUsed cgu = new ClientGrowthUsed();
//
//						cgu.setId(od.getReserved1());
//						cgu.setUpdateuser(clientid);
//						cgu.setUpdatedate(startDate);
//						cgu.setEnabled(false);
//						cgu.setIsdeleted(true);
//
//						clientGrowthService.updateByPrimaryKeySelective(cgu);
//					}

//					if(!StringUtils.isEmpty(od.getReserved2())){//优惠券使用ID
//
//						ClientRoll cr = new ClientRoll();
//
//						cr.setId(od.getReserved2());
//						cr.setUpdateuser(clientid);
//						cr.setUpdatedate(startDate);
//						cr.setEnabled(false);
//						cr.setIsdeleted(true);
//
//						clientRollService.updateByPrimaryKeySelective(cr);
//					}

					od.setUpdatedate(startDate);
					od.setUpdateuser(clientid);
					od.setOrderstatus(CommCode.SYSTEM_ORDER_STATUS_CANCEL);
					od.setEnabled(false);
					od.setIsdeleted(true);

					orderDetailService.updateByPrimaryKeySelective(od);

					mo.setUpdatedate(startDate);
					mo.setUpdateuser(clientid);
					mo.setEnabled(false);
					mo.setIsdeleted(true);

					mstOrderService.updateByPrimaryKeySelective(mo);
				} else{

					od.setUpdatedate(startDate);
					od.setUpdateuser(clientid);					
					od.setEnabled(false);
					od.setIsdeleted(true);

					orderDetailService.updateByPrimaryKeySelective(od);

					mo.setUpdatedate(startDate);
					mo.setUpdateuser(clientid);
					mo.setEnabled(false);
					mo.setIsdeleted(true);

					mstOrderService.updateByPrimaryKeySelective(mo);
				}

				resPara.setCode(CommCode.M_Y000000);
				resPara.setMessage(CommCode.M_Y000001);

				logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
			}

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
			logger.info(jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

			logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public OrderReqPara getReqPara(HttpServletRequest request) {

		OrderReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new OrderReqPara());

			reqPara = (OrderReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String odid = request.getParameter("odid");

			logger.info("reqPara userid: " + userid);
			logger.info("reqPara odid: " + odid);

			reqPara.setUserid(userid);
			reqPara.setOdid(odid);
		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		OrderReqPara orderReqPara = (OrderReqPara) reqPara;
		OrderResPara orderResPara = (OrderResPara) resPara;

		if (StringUtils.isEmpty(orderReqPara.getUserid())) {
			orderResPara.setCode(CommCode.M_A000000);
			orderResPara.setMessage(CommCode.M_P000002);
		} else if (StringUtils.isEmpty(orderReqPara.getOdid())) {
			orderResPara.setCode(CommCode.M_A000000);
			orderResPara.setMessage(CommCode.M_E000017);
		} else {
			flag = true;
		}

		return  flag;
	}

}
